﻿local _, ns = ...

--无仇恨但是目标是自己
local noThreatNpc = ns.noThreatNpc

--血条变色
function ns.UpdateHpbarColor(unitFrame,info)
	if unitFrame:IsForbidden() then return end
	if not unitFrame.unit then return end
	if UnitIsUnit(unitFrame.unit,"player") then return end
	if not UnitIsPlayer(unitFrame.unit) then
		local DB = PlateColorDB
		local CurHp = UnitHealth(unitFrame.unit) or 1
		local MaxHp = (UnitHealthMax(unitFrame.unit) and UnitHealthMax(unitFrame.unit) ~= 0) and UnitHealthMax(unitFrame.unit) or 1
		local CurHpPer = CurHp / MaxHp * 100
		local Islowhp = CurHpPer < ns.killvalue and ns.killvalue~=0
		local guid = UnitGUID(unitFrame.unit)
		local NpcId	= guid and select(6, strsplit("-", guid))
		local Threat = UnitThreatSituation("player", unitFrame.unit) or 0
		local TargetIsYou = UnitIsUnit(unitFrame.unit.."target", "player") and noThreatNpc[NpcId]
		local IsTank = UnitGroupRolesAssigned("player") == "TANK" and DB.TANKthreatColor
		local NoTank = not IsTank and DB.threatColor
		local InCombat = UnitAffectingCombat(unitFrame.unit)
		local Reaction = UnitReaction(unitFrame.unit, "player")
		local IsAttackPet = UnitPlayerControlled(unitFrame.unit.."target") and not UnitIsPlayer(unitFrame.unit.."target")

		if UnitIsTapDenied(unitFrame.unit) then--被别人先摸了没拾取
			unitFrame.healthBar:SetStatusBarColor(0.7, 0.7, 0.7)
			
		elseif Threat == 0 and InCombat and IsTank then--坦克无仇恨
			unitFrame.healthBar:SetStatusBarColor(DB["TANKnoThreatColor"]["r"],DB["TANKnoThreatColor"]["g"],DB["TANKnoThreatColor"]["b"])
			
		elseif Threat == 1 and IsTank then--坦克高仇恨
			unitFrame.healthBar:SetStatusBarColor(DB["TANKhighThreatColor"]["r"],DB["TANKhighThreatColor"]["g"],DB["TANKhighThreatColor"]["b"])
			
		elseif Threat == 2 and IsTank then--坦克仇恨降低
			unitFrame.healthBar:SetStatusBarColor(DB["TANKlowThreatColor"]["r"],DB["TANKlowThreatColor"]["g"],DB["TANKlowThreatColor"]["b"])
			
		elseif Threat == 1 and NoTank then--高仇恨
			unitFrame.healthBar:SetStatusBarColor(DB["highThreatColor"]["r"],DB["highThreatColor"]["g"],DB["highThreatColor"]["b"])
			
		elseif Threat == 2 and NoTank then--仇恨降低
			unitFrame.healthBar:SetStatusBarColor(DB["lowThreatColor"]["r"],DB["lowThreatColor"]["g"],DB["lowThreatColor"]["b"])
			
		elseif (Threat == 3 or TargetIsYou) and NoTank then--仇恨是你
			unitFrame.healthBar:SetStatusBarColor(DB["myThreatColor"]["r"],DB["myThreatColor"]["g"],DB["myThreatColor"]["b"])
		
		elseif UnitIsUnit(unitFrame.unit,"focus") and DB.myFocus then--焦点变色
			unitFrame.healthBar:SetStatusBarColor(DB["myFocusColor"]["r"],DB["myFocusColor"]["g"],DB["myFocusColor"]["b"])
			
		
		elseif Islowhp and DB.useLessHp then --斩杀
			unitFrame.healthBar:SetStatusBarColor(DB["lessHp"]["r"],DB["lessHp"]["g"],DB["lessHp"]["b"])
			
		elseif unitFrame.PCBARCOLOR then--高级自定义颜色
			if unitFrame.PCBARCOLOR.r and unitFrame.PCBARCOLOR.g and unitFrame.PCBARCOLOR.b then
				unitFrame.healthBar:SetStatusBarColor(unitFrame.PCBARCOLOR.r,unitFrame.PCBARCOLOR.g,unitFrame.PCBARCOLOR.b)
			else
				unitFrame.healthBar:SetStatusBarColor(DB["PCBARCOLOR"]["r"],DB["PCBARCOLOR"]["g"],DB["PCBARCOLOR"]["b"])
			end
			
		elseif unitFrame.useHpColor and unitFrame.useHpColor.spellId and type(DB["Auras"][unitFrame.useHpColor.spellId]) == "table" and
		DB["Auras"][unitFrame.useHpColor.spellId]["usehp"] and
		(unitFrame.useHpColor.applications >= DB["Auras"][unitFrame.useHpColor.spellId]["stack"] or unitFrame.useHpColor.applications == 0) then--光环变色
		
			local hpcid = unitFrame.useHpColor.spellId
			unitFrame.healthBar:SetStatusBarColor(DB["Auras"][hpcid]["color"]["r"],DB["Auras"][hpcid]["color"]["g"],DB["Auras"][hpcid]["color"]["b"])
		
		elseif type(DB["NPCs"][NpcId]) == "table" and DB["NPCs"][NpcId]["usehp"] then--NPC颜色
			unitFrame.healthBar:SetStatusBarColor(DB["NPCs"][NpcId]["color"]["r"],DB["NPCs"][NpcId]["color"]["g"],DB["NPCs"][NpcId]["color"]["b"])
			
		elseif Reaction and Reaction >= 5 then-- 友方npc 绿色
			unitFrame.healthBar:SetStatusBarColor(0, 1, 0)	
			
		elseif UnitIsUnit(unitFrame.unit, "target") and DB.myTarget then-- 你的目标颜色
			unitFrame.healthBar:SetStatusBarColor(DB["myTargetColor"]["r"],DB["myTargetColor"]["g"],DB["myTargetColor"]["b"])
			
		elseif (Threat == 3 or IsAttackPet or TargetIsYou) and IsTank then--坦克仇恨是你
			unitFrame.healthBar:SetStatusBarColor(DB["TANKmyhreatColor"]["r"],DB["TANKmyhreatColor"]["g"],DB["TANKmyhreatColor"]["b"])
			
		elseif Threat == 0 and InCombat and NoTank then--无仇恨
			unitFrame.healthBar:SetStatusBarColor(DB["noThreatColor"]["r"],DB["noThreatColor"]["g"],DB["noThreatColor"]["b"])
			
		elseif Reaction and Reaction == 4 then-- 中立怪 黄色
			unitFrame.healthBar:SetStatusBarColor(1, 1, 0)
			
		else
			unitFrame.healthBar:SetStatusBarColor(DB["allColor"]["r"],DB["allColor"]["g"],DB["allColor"]["b"])
		end
		
	end
end

local BarColor = CreateFrame("Frame")
BarColor:RegisterEvent("UNIT_HEALTH")
BarColor:SetScript("OnEvent", function(self, event,frame,a,b)
	if not string.match(frame,"nameplate") then return end
	local namePlate = C_NamePlate.GetNamePlateForUnit(frame,false)
	if not namePlate then return end
	local unitFrame = namePlate.UnitFrame
	if not unitFrame.unit then return end
	--要这么多b功能,还要个屁性能,统统刷tmd新
	C_Timer.After(0,function()
		ns.UpdateHpbarColor(unitFrame)
	end)
end)
hooksecurefunc("CompactUnitFrame_UpdateSelectionHighlight", function(frame)
	if not frame.unit then return end
	if frame.unit:lower():match("nameplate") then
		ns.UpdateHpbarColor(frame)
	end
end)
hooksecurefunc("CompactUnitFrame_UpdateHealthColor", function(frame)
--UNIT_NAME_UPDATE
--UNIT_THREAT_LIST_UPDATE
--UNIT_CONNECTION
	if not frame.unit then return end
	if string.match(frame.unit,"nameplate") then
		ns.UpdateHpbarColor(frame)
	end
end)
local function NoFrameUpdateHpBar()
	for i, namePlate in ipairs(C_NamePlate.GetNamePlates()) do
		ns.UpdateHpbarColor(namePlate.UnitFrame)
	end
end
local focusColor = CreateFrame("Frame")
focusColor:RegisterEvent("PLAYER_FOCUS_CHANGED")
focusColor:RegisterEvent("PLAYER_DEAD")
focusColor:RegisterEvent("PLAYER_ALIVE")
focusColor:SetScript("OnEvent", function(self, event,frame)
	if event == "PLAYER_FOCUS_CHANGED" then
		if not PlateColorDB.myFocus then return end
		NoFrameUpdateHpBar()
	else
		NoFrameUpdateHpBar()
	end
end)